// index.js
// 获取应用实例
import {
  UserService
} from '../../services/user_service';

import {
  ProductService
} from '../../services/product_service';
import getLocalByIP from '../../utils/getLocal';
import wxdata from '../../utils/wxdata';
import config from '../../config/config';

const app = getApp();

// 引入腾讯地图插件
const citySelector = requirePlugin('citySelector');


const limit = 30;
var adcode = "440100";

Page({
  data: {
    selectedCatalogId: 1,
    selectedCity: app.globalData.city,
    catalogs: [
      {
        id: 1,
        label: '回执',
        category: 'receipt',
        imagePath: '/assets/images/hz.png',
        activeImagePath: '/assets/images/hz-active.png'
      },
      {
        id: 2,
        label: '寸照',
        category: 'inch',
        imagePath: '/assets/images/cz.png',
        activeImagePath: '/assets/images/cz-active.png'
      },
      
      {
        id: 3,
        label: '签证',
        category: 'visa',
        imagePath: '/assets/images/qz.png',
        activeImagePath: '/assets/images/qz-active.png'
      },
      {
        id: 4,
        label: '考试',
        category: 'exam',
        imagePath: '/assets/images/ks.png',
        activeImagePath: '/assets/images/ks-active.png'
      },
      {
        id: 5,
        label: '近期',
        category: null,
        imagePath: '/assets/images/jq.png',
        activeImagePath: '/assets/images/jq-active.png'
      },
    ],
    products: [],
  },

  onLoad(options) {
		if(wx.getStorageSync('adcode')&&wx.getStorageSync('city')) {
			adcode = wx.getStorageSync('adcode')
			let city = wx.getStorageSync('city')
			this.getProduct(adcode, city, this.data.selectedCatalogId)
		}else {
			getLocalByIP().then(res => {
				if (res.result.ad_info.adcode <= 0) {
					adcode = 110000;
					let city = '北京市';
					this.getProduct(adcode, city, this.data.selectedCatalogId);
				} else {
					adcode = res.result.ad_info.adcode + '';
					let city = res.result.ad_info.city;
					wx.setStorageSync('city', city);
					wx.setStorageSync('adcode', adcode);
					this.getProduct(adcode, city, this.data.selectedCatalogId)
				}
			}).catch(res=>{
				console.log('res==',res);
				// adcode = 110000;
				adcode = 430103;
				let city = '长沙市';
				wx.setStorageSync('city', city);
				wx.setStorageSync('adcode', adcode);
				this.getProduct(adcode, city, this.data.selectedCatalogId)
			});			
		}

  },

  onShow() {
    const selectedCity = citySelector.getCity();
    if (selectedCity) {
      adcode = selectedCity.id;
      let city = selectedCity.fullname;
      this.getProduct(adcode, city, this.data.selectedCatalogId);
    }
  },

  // 获取商品列表
  getProduct(adcode, city, catalogId) {
    let category = this.data.catalogs[catalogId - 1].category;
    let filter = category == null ? {
      adcode,
      sortBy: 'hot:desc',
      limit: limit
    } : {
      category,
      adcode,
      limit: limit
    };
    let productService = new ProductService();
    productService.getProductList(filter).then(res => {
      this.setData({
        products: res.data.results,
        selectedCity: city,
        selectedCatalogId: catalogId
      });
      res.data.results.forEach(item => wxdata.expose(item));
    })
  },

  // 切换分类
  changeCatalog(event) {
    let catalogId = event.currentTarget.dataset.id;
    this.getProduct(adcode, this.data.selectedCity, catalogId);
  },

  // 选择城市
  chooseCity() {
    var url=`plugin://citySelector/index?key=${config.mapKey}&referer=${config.mapReferer}`
    console.log('url=',url);
    wx.navigateTo({
      url: url,
    })
  },

  // 进入商品详情页
  goToDetailPage(event) {
    let index = event.currentTarget.dataset.index;
    let product = this.data.products[index];
    if (product) {
      wx.navigateTo({
        url: '/pages/productDetail/productDetail?productId=' + product.id,
      })
    }
  },

  // 进入搜索页
  goSearchPage() {
    wx.navigateTo({
      url: `/pages/search/search?adcode=${adcode}&city=${this.data.selectedCity}`,
    })
  },

  // 查看更多
  goMore() {
    var keyword = '';
    wx.navigateTo({
      url: `/pages/search/search?adcode=${adcode}&city=${this.data.selectedCity}&keyword=${keyword}`,
    })
  },

  onShareAppMessage() {
    return {
      title: '专业机构认可 带认证回执',
      imageUrl: 'https://static.fishto.cn/images/share-thumb.png'
    }
  },

  onShareTimeline() {}

})